﻿using Leo.Blog.DAL.SysModule;
using Leo.Blog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leo.Blog.ViewModel.SysModule;

namespace Leo.Blog.BLL.SysModule
{
    public class Sys_RoleModule_B
    {
        public static List<SysRoleModuleVM> GetRoleModuleByRoleId(int roleId)
        {
            Sys_RoleModule_D _sysRoleModuleD = new Sys_RoleModule_D();
            List<Sys_RoleModule> listRoleModules = _sysRoleModuleD.LoadList(m => m.RoleId == roleId).ToList();

            List<SysRoleModuleVM> list = (from lrm in listRoleModules
                                         select new SysRoleModuleVM {
                                         Id=lrm.Id,
                                         RoleId=lrm.RoleId,
                                         ModuleId=lrm.ModuleId
                                         }).ToList();
            return list;
        }

        public static bool SaveRoleModule(int RoleId, string Modules,int userId)
        {
            Sys_RoleModule_D _sysRoleModuleD = new Sys_RoleModule_D();
            if (RoleId == 0) { return false; }
            string[] ids = Modules.Split(',');
            List<Sys_RoleModule> list = new List<Sys_RoleModule>();
            for (int i = 0; i < ids.Length; i++)
            {
                list.Add(new Sys_RoleModule { 
                RoleId=RoleId,
                ModuleId=int.Parse(ids[i]),
                CreateBy=userId,
                CreateDT=DateTime.Now
                });
            }
            bool res = _sysRoleModuleD.SaveRoleModule(RoleId, list);
            return res;
        }
    }
}
